package com.eastfair.projectcore.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.projectcore.dto.MsgTemplatePageQuery;
import com.eastfair.projectcore.dto.MsgTemplateSaveDTO;
import com.eastfair.projectcore.dto.MsgTemplateUpdateDTO;
import com.eastfair.projectcore.entity.MsgTemplate;
import com.eastfair.projectcore.service.MsgTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 消息模板表（短信、邮件、站内信）
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-08-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/msgTemplate")
@Api(value = "MsgTemplate", tags = "消息模板表（短信、邮件、站内信）")
public class MsgTemplateController extends SuperController<MsgTemplateService, Long, MsgTemplate, MsgTemplatePageQuery, MsgTemplateSaveDTO, MsgTemplateUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private MsgTemplateService msgTemplateService;

    @Override
    public void handlerResult(IPage<MsgTemplate> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<MsgTemplate> msgTemplateList = list.stream().map((map) -> {
            MsgTemplate msgTemplate = MsgTemplate.builder().build();
            //TODO 请在这里完成转换
            return msgTemplate;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(msgTemplateList));
    }


    /**
     * 条件查询
     * projectId、type、tempType
     * @return 查询结果
     */
    @ApiOperation(value = "查询模板", notes = "查询模板")
    @PostMapping("/getTemp")
    public R<Page<MsgTemplate>> getTemp(@RequestBody PageParams<MsgTemplatePageQuery> params) {
        MsgTemplatePageQuery query = params.getModel();
        QueryWrapper<MsgTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(MsgTemplate::getProjectId,query.getProjectId())
                .eq(!StrUtil.isEmpty(query.getMsgType()),MsgTemplate::getMsgType,query.getMsgType())
                .eq(query.getIsEnable() != null,MsgTemplate::getIsEnable, query.getIsEnable())
                .like(!StrUtil.isEmpty(query.getTitle()),MsgTemplate::getTitle,query.getTitle());
        return success(baseService.page(params.buildPage(),queryWrapper));
    }

    @ApiOperation(value = "保存模板", notes = "保存模板")
    @PostMapping("/saveTemp")
    public R<Boolean> saveTemp(@RequestBody MsgTemplate model) {
        return success(msgTemplateService.save(model));
    }

    @ApiOperation(value = "更新模板", notes = "更新模板")
    @PostMapping("/updateTemp")
    public R<Boolean> updateTemp(@RequestBody MsgTemplate model) {
        return success(msgTemplateService.updateById(model));
    }

}
